package com.lebang.schedule;

import com.lebang.component.RedisClient;
import com.lebang.mapper.AdRecordLogMapper;
import com.lebangweb.common.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @Author: hwq
 * @Date: 2020/12/18 10:49
 * @Version 1.0
 */
@Slf4j
@Component
public class CleanDataTask {
    @Autowired
    private AdRecordLogMapper adRecordLogMapper;

    @Autowired
    private RedisClient redisClient;


    /**
     * 每日1点删除一个月前的旧数据 和三个月前数据
     */
    @Scheduled(cron = "${task.deleteTime}")
    public void execute() {
        if (redisClient.tryGetDistributedLock("lock:Scheduled:taskdeleteTime", UUIDUtil.M(), 100000)) {
            //一个月
            LocalDateTime starTime = LocalDateTime.of(LocalDate.now().minusMonths(1), LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(LocalDate.now().minusMonths(1), LocalTime.MAX);
            //三个月
            LocalDateTime threeStarTime = LocalDateTime.of(LocalDate.now().minusMonths(3), LocalTime.MIN);
            LocalDateTime threeEndTime = LocalDateTime.of(LocalDate.now().minusMonths(3), LocalTime.MAX);
            //广告请求日志
            adRecordLogMapper.deleteByTime("ad_record_log", starTime, endTime);
            //错误码日志
            adRecordLogMapper.deleteByTime("sdk_error_log", starTime, endTime);
            //csj日志
            adRecordLogMapper.deleteByTime("csj_log", starTime, endTime);
            //删除错误码每日日志
            adRecordLogMapper.deleteByTime("sdk_error_day_log",starTime,endTime);
            //转盘奖励记录
            adRecordLogMapper.deleteByTime("member_advertisement_record", threeStarTime, threeEndTime);
            //转盘阶段奖励
            adRecordLogMapper.deleteByTime("member_ad_stage_record", threeStarTime, threeEndTime);
            //每日赚点
            adRecordLogMapper.deleteByTime("rouse_count_member_record", threeStarTime, threeEndTime);
            //每日赚点阶段
            adRecordLogMapper.deleteByTime("rouse_day_member_record", threeStarTime, threeEndTime);
            //金币流水
            adRecordLogMapper.deleteByTime("member_point_record", threeStarTime, threeEndTime);
            //消息记录
            adRecordLogMapper.deleteByTime("tb_member_notice", threeStarTime, threeEndTime);

        }
    }

    public static void main(String[] args) {
        LocalDateTime starTime = LocalDateTime.of(LocalDate.now().minusMonths(1), LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now().minusMonths(1), LocalTime.MAX);
        System.out.println(starTime);
        System.out.println(endTime);
    }
}
